import { _decorator, Component, Input, Node, UITransform, v3 } from 'cc';
import { getAdaptiveRect } from './Utils';
import { UiManager } from './UiManager';
import { GameActionQueueEnum, GameManager } from './GameManager';
import { EnemyItem } from './EnemyItem';
import { config } from './Setting';
import { HeroItem } from './HeroItem';
const { ccclass, property } = _decorator;

@ccclass('Controller')
export class Controller extends Component {
    @property(Node)
    attackNode: Node = null!;
    @property(Node)
    defenseNode: Node = null!;
    @property(Node)
    skillNode: Node = null!;

    targetNodes: Node[] = [];
    fromNode: Node = null!;
    comType: 'attack' | 'defense' | 'skill' = undefined;

    protected start(): void {
        this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }
    protected onDestroy(): void {
        this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    clear() {
        this.targetNodes = [];
        this.fromNode = null!;
        this.comType = undefined;
    }

    private onTouchStart(event): void {
        if (this.fromNode) {
            this.clear();
            return;
        }
        if (getAdaptiveRect(this.attackNode).contains(event.getUILocation())) {
            this.fromNode = (UiManager.instance as UiManager).getHeroNode((GameManager.instance as GameManager).palBattleSystem.getCurrentHero().saveData.UUID);
            this.comType = 'attack';
        }
        if (getAdaptiveRect(this.defenseNode).contains(event.getUILocation())) {
            this.fromNode = (UiManager.instance as UiManager).getHeroNode((GameManager.instance as GameManager).palBattleSystem.getCurrentHero().saveData.UUID);
            this.comType = 'defense';
        }
        if (getAdaptiveRect(this.skillNode).contains(event.getUILocation())) {
            this.fromNode = (UiManager.instance as UiManager).getHeroNode((GameManager.instance as GameManager).palBattleSystem.getCurrentHero().saveData.UUID);
            this.comType = 'skill';
        }
    }
    private onTouchMove(event): void {
        if (!this.fromNode) {
            this.clear();
            return;
        }
        this.targetNodes = [];
        (UiManager.instance as UiManager).enemyContentNode.children.forEach((node) => {
            if (getAdaptiveRect(node).contains(event.getUILocation())) {
                const targetComponent = (node.getComponent(EnemyItem) as EnemyItem);
                if (!targetComponent.data.saveData.dead) {
                    this.targetNodes.push(node);
                }
            }
        });
    }
    private onTouchEnd(event): void {
        if (!this.fromNode || !this.comType) {
            this.clear();
            return;
        }
        const gameManager = (GameManager.instance as GameManager);
        const system = gameManager.palBattleSystem;
        if (this.comType == 'skill') {
            (GameManager.instance as GameManager).openSkillDialog();
            return;
        }
        if (this.comType == 'attack') {
            if (this.targetNodes.length == 0) return;
            const targetNode = this.targetNodes[0];
            const heroUUID = system.getCurrentHero().saveData.UUID;
            const targetComponent = (targetNode.getComponent(EnemyItem) as EnemyItem);
            const enemyUUID = targetComponent.data.saveData.UUID;
            if (targetComponent.data.saveData.dead) return;
            gameManager.heroAttackEnemy(heroUUID, enemyUUID, system.getHeroAtk(heroUUID));
        }
        if (this.comType == 'defense') {
            (GameManager.instance as GameManager).heroDefense(this.fromNode.getComponent(HeroItem).data.saveData.UUID);
        }

        if (system.currentHeroIndex == system.heros.length) {
            // system.actionQueue.queue.sort((a, b) => a.order - b.order);
            // order -> hero.身法 + skill.order -> 随机值(0, 100)
            // 合击 -> 把所有的列表都清空 -> 获取到当前人物的饰品 -> 执行饰品的 callable
            system.actionQueue.play();
        } else {
            system.nextHero();
        }
        this.clear();
    }
}


